﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_FlatManger_frm_AddUserInfo : System.Web.UI.Page
{
    private void BindUserStation()
    {
        ClsStationDataDAL cls_User = new ClsStationDataDAL();
        DataSet dst = cls_User.GetStationDatas(1);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dropUserStation.Items.Clear();
            this.dropUserStation.DataTextField = "dt_name";
            this.dropUserStation.DataValueField = "dt_id";
            this.dropUserStation.DataSource = dst;
            this.dropUserStation.DataBind();
        }
        else
        {
            this.lblJs.Text = "<script>alert('获取岗位信息为空，请确保岗位信息已通过确认！')</script>";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                string strCommand = this.Page.Request.Params["Type"].ToString();

                if (strCommand == "Add")
                {
                    this.panAdd.Visible = true;
                    this.panUpdate.Visible = false;
                    this.ctxtBeginDate.Text = DateTime.Now.ToString();
                    this.lblJs.Text = "<title>添加系统用户</title>";
                    if (strUserID.Trim() != "admin") this.dropUserType.Items.RemoveAt(0);

                    this.BindUserStation();//绑定用户岗位
                }
                else
                {
                    this.panAdd.Visible = false;
                    this.panUpdate.Visible = true;
                    this.lblJs.Text = "<title>修改密码</title>";
                    this.hidUserID.Value = this.Page.Request.Params["UserID"].ToString();
                }
            }
            else
            {
                this.lblJs.Text = "";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>window.returnValue=-1;window.close();</script>";
        }
    }

    protected void btnServer_Click(object sender, EventArgs e)
    {
        ClsUserLoginDAL cls_User = new ClsUserLoginDAL();
        string strLoginName = this.txtName.Text.Trim();
        int intCount = cls_User.SysUserIDIsExist(strLoginName);

        if (intCount == 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            string strUserID = cls_Property.SetOrGetUserID;

            if (strUserID != "")
            {
                ClsMenuDAL cls_Menu = new ClsMenuDAL();
                string strUserNo = cls_Menu.GetSystemCode("0", "00", "", 51);

                if (strUserNo != "Error")
                {
                    string strWorkID = this.ctxtUsetID.Text.Trim();
                    string strLoginPwd = this.txtPwd2.Text.Trim();
                    int intPwdOverDate = Int32.Parse(this.dropPwdOverDate.SelectedItem.Value);
                    int intUserType = Int32.Parse(this.dropUserType.SelectedItem.Value);
                    string strUserStation = this.dropUserStation.SelectedItem.Value;
                    int intIsUserStop = (this.rbtnBegin.Checked) ? 0 : 1;
                    DateTime BeginDate = DateTime.Parse(this.ctxtBeginDate.Text.Trim());
                    Boolean is_Succeed = cls_User.AddSysUser(strUserNo, strWorkID, strLoginName, strLoginPwd, intPwdOverDate, intIsUserStop, intUserType, strUserStation, BeginDate, strUserID);

                    if (is_Succeed)
                    {
                        this.lblJs.Text = "<script>window.returnValue='True';window.close();</script>";
                    }
                    else
                    {
                        strUserNo = cls_Menu.GetSystemCode("1", "00", strUserNo, 51);
                        this.lblJs.Text = "<script>alert('保存失败，请重试！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text =  "<script>alert('保存失败，请重试！')</script>";
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.lblJs.Text = "<script>window.returnValue=-1;window.close();</script>";
            }
        }
        else
        {
            this.lblJs.Text =  "<script>alert('该登录名已存在，请更换其他名称！')</script>";
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string UserID = this.hidUserID.Value.Trim();
        string NewPwd = this.txtNewPwd2.Text.Trim();

        ClsUserLoginDAL cls_User = new ClsUserLoginDAL();

        if (UserID != "")
        {
            Boolean is_Succeed = cls_User.UpdateUserPwd(Int32.Parse(UserID), NewPwd);

            if (is_Succeed)
            {
                this.lblJs.Text = "<script>alert('密码修改成功！');window.close();</script>";
            }
            else
            {
                this.lblJs.Text = "<script>alert('密码修改失败，请重试！');</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('用户信息丢失，请重新打开窗口！');window.close();</script>";
        }
    }
}
